package org.ponly.cta4j.drawing;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.text.AttributedString;

import org.ponly.cta4j.drawing.background.BackgroundBrush;
import org.ponly.cta4j.drawing.filter.Filter;
import org.ponly.cta4j.drawing.font.FontFamily;
import org.ponly.cta4j.drawing.text.TextBrush;

/**
 * 可过滤 Painter, 允许对背景, 内容, 最终结果进行滤镜处理.
 *
 * @author vacoor
 * @since 1.0
 */
public class FilterablePainter extends GeneralPainter {
    private Filter backgroundFilter;
    private Filter textFilter;
    private Filter finalFilter;

    /**
     * TODO doc me.
     *
     * @param backgroundBrush backgroundBrush
     * @param textBrush       textBrush
     * @param fontFamily      fontFamily
     * @param textFilter      textFilter
     */
    public FilterablePainter(final BackgroundBrush backgroundBrush, final TextBrush textBrush, final FontFamily fontFamily,
                             final Filter textFilter) {
        super(backgroundBrush, textBrush, fontFamily);
        this.textFilter = textFilter;
    }

    /**
     * TODO doc me.
     *
     * @param backgroundBrush   backgroundBrush
     * @param textBrush         textBrush
     * @param fontFamily        fontFamily
     * @param backgroundFilters backgroundFilter
     * @param textFilter        textFilter
     * @param finalFilter       finalFilter
     */
    public FilterablePainter(final BackgroundBrush backgroundBrush, final TextBrush textBrush, final FontFamily fontFamily,
                             final Filter textFilter, final Filter backgroundFilters, final Filter finalFilter) {
        super(backgroundBrush, textBrush, fontFamily);
        this.textFilter = textFilter;
        this.backgroundFilter = backgroundFilters;
        this.finalFilter = finalFilter;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public BufferedImage paint(final BufferedImage canvas, final String text) {
        final int length = checkTextLength(text);
        final AttributedString attributedString = getAttributedString(text, length);

        BufferedImage background = doPaintBackground(canvas);

        // apply background filter
        background = applyBackgroundFilter(background);

        BufferedImage transparent = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_INT_ARGB);
        transparent = doPaintText(transparent, attributedString);

        // apply text filter
        transparent = applyTextFilters(transparent);

        final Graphics2D g2d = background.createGraphics();
        g2d.drawImage(transparent, 0, 0, null);
        g2d.dispose();

        // apply final filter
        return applyFinalFilters(background);
    }

    /**
     * 对给定背景应用背景滤镜.
     *
     * @param background 背景
     * @return 经过滤镜处理的背景
     */
    protected BufferedImage applyBackgroundFilter(final BufferedImage background) {
        return null != backgroundFilter ? backgroundFilter.apply(background) : background;
    }

    /**
     * 对给定文本应用滤镜.
     *
     * @param text 文本内容
     * @return 处理后的内容
     */
    protected BufferedImage applyTextFilters(final BufferedImage text) {
        return null != textFilter ? textFilter.apply(text) : text;
    }

    /**
     * 对给定图片应用最终处理滤镜.
     *
     * @param img 图片
     * @return 处理后内容
     */
    protected BufferedImage applyFinalFilters(final BufferedImage img) {
        return null != finalFilter ? finalFilter.apply(img) : img;
    }
}
